import { FETCH_FAVORITE_DATA } from '../actions/article';

const INITIAL_STATE = { articles: [], page: 1 };

export default function (state = INITIAL_STATE, action) {
    switch (action.type) {
        case FETCH_FAVORITE_DATA:
            if ((action.payload && action.payload.data && action.payload.data.page && action.payload.data.page === 1)
                || action.payload.status != 200 || action.payload.data.articles == undefined) {
                state.articles = [];
                state.page = 1;
                state.pageCount = 1;
            }
            if (action.payload && action.payload.data && action.payload.data.articles && action.payload.data.articles.length > 0) {

                state.articles = action.payload.data.articles;
                state.page = action.payload.data.page;
                state.pageCount = action.payload.data.pageCount;
            }
            return { ...state };
        default:
            return state;
    }
}

